#include <stdio.h>
#define MAX 100
#define MAXCOST 0x7fffffff

int graph[MAX][MAX];

void update(int indegree[],int graph[][MAX],int j,int n){
	indegree[j] = -1;
	for (int i = 1; i <= n; ++i)
	{
		if(graph[j][i]<MAXCOST)
			indegree[i]--;
	}
}
void topological(int graph[][MAX],int n){
	int indegree[n+1];
	for (int i = 0; i < n+1; ++i)
	{
		indegree[i] = 0;
	}
	for (int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n; ++j){
			if(i == j)
				continue;
			if(graph[i][j] < MAXCOST)
				indegree[j]++;
		}
	}
	for (int i = 0; i < n; ++i)
	{
		for(int j = 1; j <= n ; ++j){
			if(indegree[j]==0){
				printf("%d ",j);
				update(indegree,graph,j,n);
			}
		}
	}
	printf("\n");

}

void display(int graph[][MAX],int n){
	printf("您输入的矩阵是：\n");
	for (int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n ; ++j){
			if(graph[i][j]==MAXCOST){
				printf("@ ");
				continue;
			}
			printf("%d ",graph[i][j]);
		}
		printf("\n");
	}
}
int main(){
	int i, j , k , m , n;
	int x, y, cost;
	scanf("%d%d",&m,&n);
	for(i = 1; i <= m; i ++){
		for(j = 1; j <= m; j++){
			graph[i][j] = MAXCOST;
		}
	}
	for(k = 1; k <= n; k++){
		scanf("%d%d%d",&i,&j,&cost);
		graph[i][j] = cost;
	}
	display(graph,m);
	topological(graph,m);	
	return 0;

}